import request from '@/utils/request';
import {EnvShow, EnvCreateParam, EnvDeleteParam, EnvContent} from './data.d';

export async function queryEnvs() {
  return request('/api/v1/elasticenv/envs')
    .then(function(response){
      if (response.data.length === 0) {
        return {
          data: [],
          success: true,
          total: 0,
        };
      }
      // process data
      const outData: EnvShow[] = [];
      response.data.forEach((eachData: EnvContent) => {
        outData.push({
          id: eachData.id,
          name: eachData.name,
          date: eachData.createdAt,
          user: eachData.createdBy,
          userUrl: `${eachData.customerSubDomain  }.${  eachData.mainDomain}`,
          adminUrl: `${eachData.adminSubDomain  }.${  eachData.mainDomain}`,
          mobileUrl: `${eachData.mobileSubDomain  }.${  eachData.mainDomain}`,

          accessIP: eachData.accessIP,
          dbName: eachData.dbName,
          dbUser: eachData.dbUser,
          dbPort: eachData.dbPort,
          redisPort: eachData.redisPort,

          jenkinsState: eachData.jenkinsCreateState,
          nginxState: eachData.nginxCreateState,
          dbState: eachData.dbCreateState,
          redisState: eachData.redisCreateState
        });
      });
      return {
        data: outData,
        success: true,
        total: outData.length,
      };
    })
    .catch((error) => {
      console.log(error);
      return {
        data: [],
        success: true,
        total: 0,
      };
    });
}

export async function createEnv(params: EnvCreateParam) {
  return request('/api/v1/elasticenv/env/create', {
    method: 'POST',
    data: {
      ...params
    },
  });
}

export async function removeEnv(params: EnvDeleteParam) {
  return request('/api/v1/elasticenv/env/delete', {
    method: 'POST',
    data: {
      ...params,
    },
  });
}

